/*引自：https://github.com/mylen/leaflet.TileLayer.WMTS*/
/* eslint-disable */
L.TileLayer.WMTS = L.TileLayer.extend({
    defaultWmtsParams: {
        service: 'WMTS',
        request: 'GetTile',
        version: '1.0.0',
        layers: '',
        styles: '',
        tilematrixSet: '',
        format: 'image/jpeg'
    },
    initialize: function (url, options) {
        this._url = url;
        var wmtsParams = L.extend({}, this.defaultWmtsParams);
        var tileSize = options.tileSize || this.options.tileSize;
        if (options.detectRetina && L.Browser.retina) {
            wmtsParams.width = wmtsParams.height = tileSize * 2;
        } else {
            wmtsParams.width = wmtsParams.height = tileSize;
        }
        for (var i in options) {
            if (!Object.prototype.hasOwnProperty.call(this.options, i) && i != "matrixIds") {
                wmtsParams[i] = options[i];
            }
        }
        this.wmtsParams = wmtsParams;
        this.matrixIds = options.matrixIds || this.getDefaultMatrix();
        L.setOptions(this, options);
    },
    onAdd: function (map) {
        this._crs = this.options.crs || map.options.crs;
        L.TileLayer.prototype.onAdd.call(this, map);
    },
    getTileUrl: function (coords) {
        var tileSize = this.options.tileSize;
        var nwPoint = coords.multiplyBy(tileSize);
        nwPoint.x += 1;
        nwPoint.y -= 1;
        var sePoint = nwPoint.add(new L.Point(tileSize, tileSize));
        var zoom = this._tileZoom;
        var nw = this._crs.project(this._map.unproject(nwPoint, zoom));
        var se = this._crs.project(this._map.unproject(sePoint, zoom));
        window.tilewidth = se.x - nw.x;
        var ident = this.matrixIds[zoom].identifier;
        var tilematrix = ident; //this.wmtsParams.tilematrixSet + ":" + ident  //bug在加载天地图时会出错，在加载geoserver发布的wmts时又有用
        var X0 = this.matrixIds[zoom].topLeftCorner.lng;
        var Y0 = this.matrixIds[zoom].topLeftCorner.lat;
        var tilecol = Math.floor((nw.x - X0) / window.tilewidth);
        var tilerow = -Math.floor((nw.y - Y0) / window.tilewidth);
        var url = L.Util.template(this._url, {
            s: this._getSubdomain(coords)
        });
        if (this.options.layer == "onemap") {
            return url + "/" + tilematrix + "/" + tilerow + "/" + tilecol + "?random=" + new Date().getTime();
        } else {
            return url +
                L.Util.getParamString(this.wmtsParams, url) +
                "&tilematrix=" +
                tilematrix +
                "&tilerow=" +
                tilerow +
                "&tilecol=" +
                tilecol + "&random=" + new Date().getTime();
        }
    },
    setParams: function (params, noRedraw) {
        L.extend(this.wmtsParams, params);
        if (!noRedraw) {
            this.redraw();
        }
        return this;
    },
    getDefaultMatrix: function () {
        return [{
            "identifier": 1,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 2,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 3,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 4,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 5,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 6,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 7,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 8,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 9,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 10,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 11,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 12,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 13,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 14,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 15,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 16,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 17,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 18,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 19,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 20,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 21,
            topLeftCorner: new L.LatLng(90, -180)
        }, {
            "identifier": 22,
            topLeftCorner: new L.LatLng(90, -180)
        }];
    }
});
L.tileLayer.wmts = function (url, options) {
    return new L.TileLayer.WMTS(url, options);
};
